package com.ipan.weixin.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;

/**
 * XML操作工具类
 * 
 * @author iPan
 * @version 2013-12-25
 */
public class WxXmlUtils {

	public static Document read(String text) {
		return read(text, "utf-8");
	}
	
	public static Document read(String text, String charsetName) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader();
			document = reader.read(new ByteArrayInputStream(text.getBytes(charsetName)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static Document read(File file, String charsetName) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader();
			reader.setEncoding(charsetName);
			document = reader.read(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static Document read(InputStream in, String charsetName) {
		Document document = null;
		try {
			SAXReader reader = new SAXReader();
			reader.setEncoding(charsetName);
			document = reader.read(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}

}
